package com.polaris.live.adapter.home

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.adapter.live.HomeBannerAbsAdapter
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.constant.PagConst.FREE_CARD_PAG
import com.polaris.live.common.constant.PagConst.HOME_FREE_CALL_PAG
import com.polaris.live.common.ext.dp
import com.polaris.live.databinding.AdapterHotListManBinding
import com.polaris.live.databinding.AdapterListBannerBinding
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.image.loadWejoyImage
import com.polaris.live.utils.route.RouterUtils
import com.polaris.live.utils.state.UserStateUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * HomeManAdapter
 *
 * @author Created by lucas on 2023/10/17 15:12
 * @since 1.0.0
 */
class HomeManAdapter : BaseAdapter<Any, BaseViewHolder>(0) {

    init {
        addChildClickViewIds(R.id.bg_call, R.id.user_cover)
    }

    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is DisplayUserBean -> TYPE_NORMAL
            is ListWithBannerBean -> TYPE_BANNER
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            TYPE_BANNER -> BannerViewHolder(
                AdapterListBannerBinding.inflate(layoutInflater, parent, false)
            )

            TYPE_NORMAL -> NormalManViewHolder(
                AdapterHotListManBinding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is NormalManViewHolder -> if (item is DisplayUserBean) holder.bind(item)
            is BannerViewHolder -> if (item is ListWithBannerBean) holder.bind(item)
        }
    }

    class NormalManViewHolder(
        binding: AdapterHotListManBinding,
    ) : BaseBindingViewHolder<AdapterHotListManBinding, DisplayUserBean>(binding) {

        override fun bind(item: DisplayUserBean) {
            mBinding.apply {
                Glide.with(mBinding.root.context).loadWejoyImage(item.cover)
                    .transform(CenterCrop(), RoundedCorners(2.dp)).into(userCover)

                nickName.text = item.base.nickname

                if (TextUtils.isEmpty(item.base.country)) {
                    userCountry.visibility = View.GONE
                } else {
                    userCountry.visibility = View.VISIBLE
                    userCountry.setCountry(item.base.country)
                }
                val languageCode = item.base.language
                val isNull = languageCode == "null" || languageCode == "NULL"
                if (languageCode != null && !isNull) {
                    mBinding.llLanguage.visibility = View.VISIBLE
                    val languageName = CommonUtils.getLanguageNameFromLocaleCode(languageCode)
                    if (languageName.isEmpty()) {
                        mBinding.tvLanguage.visibility = View.GONE
                    } else {
                        mBinding.tvLanguage.text = CommonUtils.getLanguageNameFromLocaleCode(languageCode)
                    }
                } else {
                    mBinding.llLanguage.visibility = View.GONE
                }
                mBinding.widgetLevel.setRichCharmBySexLevel(
                    item.base.sex,
                    item.base.richs,
                    item.base.charms
                )

                if (item.base.callPrice != null) {
                    mBinding.tvCallPrice.text = "${item.base.callPrice}/min"
                    mBinding.llCallPrice.visibility = View.VISIBLE
                } else {
                    mBinding.llCallPrice.visibility = View.GONE
                }

                svState.setState(item.base, item)

                pagView.apply {
                    path = HOME_FREE_CALL_PAG
                    setRepeatCount(-1)
                    play()
                }

                //免费
                if (item.actualPrice == 0L) {
                    call.setBackgroundResource(R.drawable.btn_call_free)
                    callIcon.apply {
                        visibility = View.VISIBLE
                        path = FREE_CARD_PAG
                        setRepeatCount(-1)
                        play()
                    }
                    call.setBackgroundResource(R.drawable.btn_call_free)
                    callIcon.visibility = View.VISIBLE
                } else {
                    call.setBackgroundResource(R.drawable.btn_call_pay)
                    callIcon.visibility = View.GONE
                }

                if (UserStateUtils.callUserState(item.base) == AppConst.YES) {
                    bgCall.visibility = View.VISIBLE
                } else {
                    bgCall.visibility = View.GONE
                }
            }
        }
    }

    companion object {

        const val TYPE_BANNER = 0
        const val TYPE_NORMAL = 1
    }
}

class BannerViewHolder(binding: AdapterListBannerBinding) :
    BaseBindingViewHolder<AdapterListBannerBinding, ListWithBannerBean>(binding) {

    override fun bind(item: ListWithBannerBean) {
        val adapter = HomeBannerAbsAdapter()
        mBinding.homeAbsBanner.apply {
            setAdapter(adapter)
            setIndicatorVisibility(View.GONE)
            setInterval(5000)
            if (CommonUtils.isMan(UserManager.getUser().sex)) {
                setRoundCorner(2.dp)
            } else {
                setRoundCorner(8.dp)
            }
            setOnPageClickListener { _, position ->
                GlobalPopupManager.showCompletePopup(context, isOk = {
                    val itemData = item.banners.getOrNull(position) ?: return@showCompletePopup
                    val link = itemData.link.takeIf { it?.isNotEmpty() == true }
                        ?: itemData.nativeLink.takeIf { it?.isNotEmpty() == true }
                    link?.let { RouterUtils.routerGoto(it) }
                })
            }
            create(item.banners)
        }
    }
}